package com.leng.test.resolver;

import java.util.HashMap;
import java.util.Map;

import com.alibaba.fastjson.JSONObject;
import com.leng.web.annotation.exception.resolver.HandlerExceptionResolver;
import com.leng.web.handler.Handler;
import com.leng.web.request.WebRequest;
import com.leng.web.util.HttpResponseUtils;

import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.http.HttpResponseStatus;

/**
 * 统一的异常处理器
 * 
 * @author wyy
 *
 */
public class MyHandlerExcetionResolver implements HandlerExceptionResolver {

	@Override
	public void resolveException(WebRequest request, Handler handler, ChannelHandlerContext ctx, Exception e) {
		System.err.println("异常处理器捕获到异常了");
		e.printStackTrace();
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("error", "程序发生异常，被统一的异常处理器捕获了");
		String message = JSONObject.toJSONString(map);
		HttpResponseUtils.sendHttpResponse(ctx, HttpResponseStatus.OK, message, "application/json");
	}

}
